using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;

namespace Scurvy.Media.Pipeline
{
    [ContentTypeWriter]
    internal class VideoStreamWriter : ContentTypeWriter<VideoStreamContent>
    {
        protected override void Write(ContentWriter output, VideoStreamContent value)
        {
            System.Diagnostics.Debug.WriteLine("Writing Video Stream");
            
            output.Write(value.Count);
            output.Write(value.FrameRate);
            
            output.Write(value.Size);

            for (int i = 0; i < value.Frames.Count; i++)
            {
                System.Diagnostics.Debug.WriteLine("\ttex#" + i.ToString());
                Texture2DContent tex = value.Frames[i];
                byte[] pix = tex.Faces[0][0].GetPixelData();
                
                //tex.Faces[0].Clear();
                //tex.Faces[0] = null;
                //tex.Faces.Clear();
                tex = null;
                value.Frames[i] = null;
                
                //GC.Collect();
                output.Write(pix.Length);
                output.Write(pix);

            }

            value.Dispose();
        }

        public override string GetRuntimeReader(Microsoft.Xna.Framework.TargetPlatform targetPlatform)
        {
            return typeof(VideoStreamReader).AssemblyQualifiedName;
        }

        public override string GetRuntimeType(Microsoft.Xna.Framework.TargetPlatform targetPlatform)
        {
            return typeof(VideoStream).AssemblyQualifiedName;
        }
    }
}
